﻿/*
 * Copyright 2008 Pimm Hogeling, Karim Sempel and Demian Sempel
 * 
 * This file is part of 8bitplays.
 * 
 * 8bitplays is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * 8bitplays is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with 8bitplays. If not, see <http://www.gnu.org/licenses/>.
 */
package nl.sandmen.team.playlist {
	import nl.sandmen.team.data.Album;
	import nl.sandmen.team.main.DataManager;
	/**
	* @author Team Sandmen
	*/
	public class Playlist {
		private var currentIndex:uint = 0;
		private var list:Array;
		public function Playlist(albumSource:Array):void {
			list = new Array();
			while(albumSource.length) {
				list.push(albumSource.splice(uint(albumSource.length * Math.random()), 1)[0]);
			}
		}
		public function get afterNextAlbum():Album {
			return list[wrappedIndex(currentIndex + 2)];
		}
		public function get beforePreviousAlbum():Album {
			return list[wrappedIndex(currentIndex - 2)];
		}
		public function get currentAlbum():Album {
			return list[currentIndex];
		}
		public function goNextAlbum():void {
			if (++currentIndex === list.length) {
				currentIndex = 0;
			}
		}
		public function goPreviousAlbum():void {
			if (currentIndex-- === 0) {
				currentIndex = list.length - 1;
			}
		}
		public function get nextAlbum():Album {
			return list[wrappedIndex(currentIndex + 1)];
		}
		public function get previousAlbum():Album {
			return list[wrappedIndex(currentIndex - 1)];
		}
		private function wrappedIndex(index:int):uint {
			if (index < 0) {
				return list.length + index;
			}
			if (index >= list.length) {
				return index - list.length;
			}
			return index;
		}
	}
}